/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 微商管理
 *  */
import { 
  editDerivativeInfo,
  deleteDerivativeInfo,
  addDerivativeInfo,
  getDerivativeManagement,
  wsSearchPhone,
  wsSearchId
} from '@/services/orderManagement';
import {message} from 'antd';
export default {
  namespace: "derivativeManage",
  state: {
    derivativeManageList: {
      list: [],
      pagination: null,
    },
    cascaderInfo:[],
    wsByPhoneSearchInfo:{}
  },
  effects: {
    *getDerivativeManagement({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(getDerivativeManagement, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { list, count, page_size, current_page } = data;
      if(code == 0){
        yield put({
            type: "save",
            payload: {
              derivativeManageList:{
                list: list || [],
                pagination:{
                  total:count,
                  pageSize: page_size,
                  current: current_page,
                  showQuickJumper:false,
                  showSizeChanger:false,
                }
              }
            },
        });
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    *wsSearchId({payload, __dva_resolve, __dva_reject}, { call, put }){
      const response = yield call(wsSearchId, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if(code == 0){
        __dva_resolve(data[0])
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    *wsSearchPhone({payload, __dva_resolve, __dva_reject}, { call, put }){
      const response = yield call(wsSearchPhone, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if(code == 0&&data.is_register==1){
        __dva_resolve(data.info)
      }else {
        if(code!=0){
          message.error(msg);
        }
        __dva_reject();
      }
    },
    *editDerivativeInfo({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(editDerivativeInfo, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if(code == 0){
        message.success(msg)
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    *deleteDerivativeInfo({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(deleteDerivativeInfo, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if(code == 0){
        message.success('删除成功')
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    },
    *addDerivativeInfo ({ payload, __dva_resolve, __dva_reject }, { call, put }){
      const response = yield call(addDerivativeInfo, payload);

      if (!response) return;
      const { code, data, msg } = response;

      if(code == 0){
        message.success(msg)
        __dva_resolve(response);
      }else{
        __dva_reject();
        message.error(msg);
      }
    }
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  }
}